package com.yanqu.road.server.gameplayer.module.union.cross;

import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.pb.common.CommonProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 商会贸易（商贸）
 */
public class CrossUnionTradeModule extends GeneralModule {

    public CrossUnionTradeModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public boolean saveData() {
        return true;
    }

    @Override
    public void afterLogin() {

        String unionUid = player.getUserInfo().getUnionUid();
        if(StringUtils.isNullOrEmpty(unionUid)){
           return;
        }
        //UnionTradeMgr.resetData(unionUid);       //检测跨天的商贸（待前端对接次数开启后可删除）
        //todo sendMessage
        syncCanJoinRedHot();
    }

    public void syncCanJoinRedHot() {
        String unionUid = player.getUserInfo().getUnionUid();
        if (StringUtils.isNullOrEmpty(unionUid)) {
            return;
        }
//        boolean canJoin = UnionTradeMgr.hasRouteCanJoin(unionUid, player);
//        WorldMessageProto.RedHotNotifyMsg.Builder redHotMsg = WorldMessagePb.parseRedHotNotifyMsg(eRedHotNotifyType.UnionTradeCanJoin.getValue(), canJoin == true ? 1 : 0, null);
//        player.sendChannelPacket(Protocol.U_RED_HOT_NOTIFY, redHotMsg);
        //todo sendMessage
        player.sendPacket(Protocol.C_UNION_TRADE_SYNC_RED_DOT, CommonProto.NullValueMsg.newBuilder());
    }

    public void resetOneDay(){
        PatronsModule patronsModule = player.getModule(PatronsModule.class);
        List<UserPatrons>  userPatronsList = patronsModule.getUserPatronsList();
        UserPatrons userPatrons;
        for(UserPatrons tempPatrons : userPatronsList){
            userPatrons = patronsModule.getUserPatrons(tempPatrons.getPatronsId());
            if (!DateHelper.isToday(new Date(userPatrons.getResetTime()))) {
                userPatrons.setResetTime(System.currentTimeMillis());
                userPatrons.setUnionTradeSend(false);
            }
        }
    }

    //获取用户派遣门客
    public List<Integer> getSendPatronsIdList() {
        List<Integer> hadSend = new ArrayList<>();
        List<UserPatrons>  userPatronsList = player.getModule(PatronsModule.class).getUserPatronsList();
        for(UserPatrons userPatrons : userPatronsList){
            if(userPatrons.isUnionTradeSend()) {
                hadSend.add(userPatrons.getPatronsId());
            }
        }
        return hadSend;
    }


}
